//import fs from 'node:fs'
import { ShellExecution, Task, tasks, ProgressLocation, window, workspace, Uri } from 'vscode'
import { SerialPort } from "serialport";
import { glob } from "glob";
const fs = require('fs');
const path = require('path');

export const runUpload = (serialport:SerialPort) => {

	const workspaceFolders = workspace.workspaceFolders;
	if (!(workspaceFolders && workspaceFolders.length > 0)) {
		return
	} 
	const rootPath = workspaceFolders[0].uri.fsPath;
	//let fff=new String(await workspace.fs.readFile(Uri.file(rootPath+ 'a.txt')))
	//console.log("文件5",fff)

	let f=workspace.fs.readFile(Uri.file(rootPath+ '\a.txt')).then(data=>{
		console.log("文件0",new String(data))
	})
	
	traverseDirectory(rootPath+"/build", function(path,file) {  
		console.log("文件0",path)
	
		fs.readFile(path,'utf-8',function(err: NodeJS.ErrnoException | null, data: Buffer) {
			console.log(err);
			console.log('-----------');
			console.log(data);
		})

		const txt = fs.readFileSync(path, 'utf8');
		//const base64Image = imageBuffer.toString('base64');
		window.showInformationMessage("正在上传:"+path);
		let data="upload.py file "+ txt
		//serialport.write(data)
		window.showInformationMessage("上传完成:"+data);
	});
}

function traverseDirectory(dir:String, callback: (path:string,file:string) => void) {
  fs.readdirSync(dir).forEach((file:string) => {
    const fullPath = path.join(dir, file);
    if (fs.lstatSync(fullPath).isDirectory()) {
      // 如果是文件夹，则递归遍历
      traverseDirectory(fullPath, callback);
    } else {
      // 如果是文件，则执行回调函数
      callback(fullPath,file);
    }
  });
}